package com.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dao.BusinessDAO;
import com.pojo.Business;
import com.pojo.Dictionary;
import com.pojo.Staff;
import com.util.ValidateHelper;
import com.vo.BusinessVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class BusinessService extends ServiceImpl<BusinessDAO, Business> {

    @Autowired
    private BusinessDAO businessDAO;

    @Autowired
    private StaffService staffService;

    @Autowired
    private DictionaryService dictionaryService;

    public IPage<BusinessVo> getBusinessList(String name, Integer page, Integer limit) {
        if (ValidateHelper.isEmptyString(name)) {
            name = null;
        }
        Page p = new Page(page, limit);

        IPage<BusinessVo> iPage = businessDAO.selectBusiness(p, name);
        return iPage;
    }

    public Business selectBusinessByBh(Integer bh){
        List<Business> businessList = businessDAO.selectList(new QueryWrapper<Business>().eq("bh", bh));
        if (ValidateHelper.isNotEmptyCollection(businessList)) {
            return businessList.get(0);
        }
        return null;
    }

    public int selectBh(Integer bh){
        return businessDAO.selectList(new QueryWrapper<Business>().eq("bh", bh)).size();
    }

    public int selectBusinessName(String name){
        return businessDAO.selectList(new QueryWrapper<Business>().eq("name", name)).size();
    }

    public int selectBusinessNameId(String name,String id){
        return businessDAO.selectList(new QueryWrapper<Business>().eq("name", name).eq("id", id)).size();
    }

    public int selectBhId(Integer bh, String id){
        return businessDAO.selectList(new QueryWrapper<Business>().eq("bh", bh).eq("id", id)).size();
    }

    public List<Business> getBusinessList(){
        List<Business> businessList = businessDAO.selectList(new QueryWrapper<Business>().eq("status" , 1)) ;
        return businessList;
    }

    public List<BusinessVo> exportByExcel(String name) {
        if (ValidateHelper.isEmptyString(name)) {
            name = null;
        }
        List<BusinessVo> businessList = businessDAO.selectBusinessList(name);
        return businessList;
    }


    public Integer getBh(Integer bh) {
        Business business = selectBusinessByBh(bh);
        if (business != null) {
            return getBh(bh + 1);
        }else {
            return bh;
        }
    }

    public int getMaxBh() {
        QueryWrapper q = new QueryWrapper();
        q.orderByDesc("bh");
        q.last("limit 1");
        Business business = businessDAO.selectOne(q);
        if (business == null) {
            return 1000;
        }
        return business.getBh();
    }


    public String getQrCode (String staffId) {
        Staff staff = staffService.selectID(staffId);
        if (staff.getWhiteList() == 1) {
            return "red";
        }else {
//            Hesuan hesuan = hesuanService.getLastResultHs(staffId);
            if (ValidateHelper.isNotEmptyString(staff.getQrcode())) {
                if (null != staff.getWhite_time() && null != staff.getQrCodeExpDay()) {
//                    if (null == hesuan || null == hesuan.getCreateTime() || (null != hesuan.getCreateTime() && hesuan.getCreateTime().before(staff.getWhite_time()))) {
                        if (null != staff.getWhite_time() && null != staff.getQrCodeExpDay()) {
                            Date curDate = new Date();
                            long days = curDate.getTime() - staff.getWhite_time().getTime();
                            if (days <= (staff.getQrCodeExpDay().longValue() * (1000*3600*24L))) {
                                return staff.getQrcode();
                            }
                        }
//                    }
                }
            }
        }

        Integer orangeDay = 3;
        if ("工商从业人员".equals(staff.getZhiye())) {
            List<Dictionary> dictionaryList = dictionaryService.getDicByType("GsOrangeDay");
            if (ValidateHelper.isNotEmptyCollection(dictionaryList)) {
                orangeDay = Integer.valueOf(dictionaryList.get(0).getValue());
            }
        }else if ("特种行业从业人员".equals(staff.getZhiye())) {
            List<Dictionary> dictionaryList = dictionaryService.getDicByType("TzOrangeDay");
            if (ValidateHelper.isNotEmptyCollection(dictionaryList)) {
                orangeDay = Integer.valueOf(dictionaryList.get(0).getValue());
            }
        }else {
            List<Dictionary> dictionaryList = dictionaryService.getDicByType("OrangeDay");
            if (ValidateHelper.isNotEmptyCollection(dictionaryList)) {
                orangeDay = Integer.valueOf(dictionaryList.get(0).getCode());
            }
        }

        if (null != staff.getJcFlag()) {
            if (staff.getJcFlag() == 1) {
//                List<Hesuan> hesuanList = hesuanService.selectHs(staff.getId());
//                if (hesuanList.size() > 0) {
//                    if (hesuanList.get(0).getJcResult() == 1) {
//                        Date curDate = new Date();
//                        long days = curDate.getTime() - hesuanList.get(0).getCreateTime().getTime();
//                        if (days <= (hesuanList.get(0).getExpDay().longValue() * (1000*3600*24L))) {
//                            return "green";
//                        }else if (days <= ((hesuanList.get(0).getExpDay().longValue() + orangeDay.longValue()) * (1000*3600*24L))) {
//                            return "orange";
//                        }else {
//                            return "red";
//                        }
//                    }else if(hesuanList.get(0).getJcResult() == 2){
//                        return "red";
//                    }else {
//                        Hesuan hesuan = hesuanService.selectHsByStaff(staff.getId());
//                        if (null != hesuan) {
//                            if (hesuan.getJcResult() == 1) {
//                                Date curDate = new Date();
//                                long days = curDate.getTime() - hesuan.getCreateTime().getTime();
//                                if (days <= (hesuan.getExpDay().longValue() * (1000*3600*24L))) {
//                                    return "green";
//                                }else if (days <= ((hesuan.getExpDay().longValue() + orangeDay.longValue()) * (1000*3600*24L))) {
//                                    return "orange";
//                                }else {
//                                    return "red";
//                                }
//                            }else {
//                                return "red";
//                            }
//                        }else {
//
//                            return "red";
//                        }
//                    }
//
//                }
            }
        }
        return "red";
    }
}
